/*
 * app_filetransport_api_subsidiary.h
 *
 *  Created on: 2023年8月30日
 *      Author: HXF
 */

#ifndef DS_APP_INS_APP_FILETRANSPORT_API_SUBSIDIARY_H_
#define DS_APP_INS_APP_FILETRANSPORT_API_SUBSIDIARY_H_

#include "app_filetransport_def.h"
#include "device.h"
#include "stdbool.h"
#include "stddef.h"

unsigned int get_msg_crc32(unsigned char *msg, int len);
void file_api_test(void);

typedef struct{
  int file_page_num;
  int remain_f_p_num;
  unsigned char block_id[2];
  unsigned char block_page_num[2];
  unsigned char remain_b_p_num[2];
}product_targets;

typedef struct{
  unsigned char block_id;         //文件块ID
  unsigned char b_result_info[4];
}custom_block_info;

typedef struct{
  int file_page_num;
  int remain_f_c_num;
#ifdef HUB_DEVICE
  unsigned char block_idx_pre;
  unsigned char remain_b_c_num;
  unsigned char block_id;
  unsigned char block_page_num;
#else
  custom_block_info block_info[5];
  unsigned char finish_flag;  //0,没结束；1，校验成功，2校验失败
  unsigned char verify_mode;  //
#endif
}custom_targets;

#ifdef HUB_DEVICE
int start_fix_time_to_tx_file_Timer(void);
int stop_fix_time_to_tx_file_Timer(void);
int start_qrfh_handshake_Timer(void);
int stop_qrfh_handshake_Timer(void);

int update_ota_rfm_request_state(unsigned char rfmid, unsigned char state);
int update_ota_rfm_confirm_state(unsigned char rfmid, unsigned char state);
int update_ota_rfm_check_state(unsigned char rfmid, unsigned char state,
                               unsigned char *blockvalue, int len);
int update_all_ota_rfm_finish_state(unsigned char state);
bool check_ota_rfm_all_request(void);
bool check_ota_rfm_all_confirm(void);
bool check_ota_rfm_all_check(void);
bool check_ota_rfm_all_finish(void);
int update_ota_rfm_id_buf(void);

void ota_init_production(void);
int get_ota_production_blockid(void);
bool check_file_is_exist_in_filelink(unsigned char *pageid, FILE_LINK_NUM_E num);
int ota_update_production_progress(FILE_LINK_NUM_E num);
bool check_ota_start_production(void);
bool check_ota_suspend_production(void);
bool check_ota_finish_production(void);
bool check_ota_production_no_run(void);
int ota_update_custom_progress(void);
bool check_ota_start_custom(void);
bool check_ota_suspend_custom(void);
int get_ota_production_file_link_custom_num(unsigned char blockid);
void set_ota_production_file_link_custom_blockid(unsigned char blockid);
int get_ota_production_file_link_custom_blockid(void);
int update_history_file_link(void);
void printf_rfm_ota_info(void);

void ota_custom_cache_history(FILE_LINK_NUM_E num, unsigned char *ptr_file, int len);
void creat_ota_finish_evt_to_panel(void);

#else
void ota_init_custom(void);
int start_timer_to_trigger_confirm(void);
int start_timer_to_check_no_rcv_timeout(void);
int start_timer_to_delay_app_install_and_reboot(void);
bool deal_file_payload(unsigned char *pageid, unsigned char *file, int file_len,
                       unsigned char *valid_pageid, unsigned char *valid_file,
                       int *valid_len);
bool check_custom_receve_file_finish(void);
void set_custom_file_finish_flag(unsigned char flag);
unsigned char get_custom_file_finish_flag(void);
unsigned char get_custom_file_verify_mode(void);
unsigned char*  get_custom_file_verify_code(void);
int packet_check_response(unsigned char *packet, unsigned char *packet_len);
#endif

void ota_enter_hook(void);
void ota_abort_hook(void);

FILE_TRANSPORT_ROLE_E my_transport_role(unsigned char type);
int filepayload_cache_to_file_link(unsigned char *payload, int payload_len);
int get_blockid_by_pageid(unsigned char *pageid);
bool chcek_block_match_by_pageid(unsigned char *pageid);
bool chcek_file_is_exist_by_pageid(unsigned char *pageid);
bool check_file_crc32_is_sample(unsigned char *file,int file_len,
                                unsigned char *crc32_buf, int crc32_len);
int creat_ota_start_event(void);
int creat_ota_abort_event(void);
int creat_ota_finish_event(void);
int creat_ota_wait_finish_event(void);
int creat_ota_wait_timeout_event(void);
int creat_ota_tx_notify_event(void);
int creat_ota_tx_file_page_event(void);
int creat_ota_tx_file_check_event(void);
int creat_ota_rx_file_page_event(void);
int creat_ota_rx_file_check_event(void);
//void ota_init_custom(void);

bool check_ota_need_abort(void);

int get_file_ctrl_duty(unsigned char *duty);
int get_file_file_size(unsigned char *file_size);
int get_file_page_size(unsigned char *page_size);
int get_file_sha256_code(unsigned char *sha256_code);

#endif /* DS_APP_INS_APP_FILETRANSPORT_API_SUBSIDIARY_H_ */
